<?php
/**
 * Vendor1协议报文解析测试
 * 测试原有协议是否正常工作
 */

echo "========================================\n";
echo "Vendor1协议报文解析测试\n";
echo "========================================\n\n";

// 真实的MQTT主题
$topic = 'tc/device/out/00905BD7487C';

// 真实的Vendor1消息数据
$messageData = [
    'api_version' => '1.0',
    'upload_data_type' => 'normal',
    'cur_sim_id' => 0,
    'device_info' => [
        'dev_imei1' => '863874075005318',
        'dev_imei2' => '863874075060313',
        'dev_sn' => '201343080',
        'dev_iccid' => '898608551024D1344855',
        'dev_mac' => '00:90:5B:D7:48:7C',
        'dev_sw_version' => 'V1.0',
    ],
    'sim_info' => [
        [
            'sim_id' => 0,
            'sim_exist' => 1,
            'sim_signal_level' => 5,
            'sim_dbm' => '-84dBm',
            'sim_iccid' => '898608551024D1344855',
            'sim_mnc' => '0',
            'sim_operator_info' => 'CMCC',
        ],
        [
            'sim_id' => 1,
            'sim_exist' => 0,
        ]
    ],
    'hotspot_info' => [
        'hotspot_name' => 'zzzzzzzpppppp',
        'hotspot_password' => 'zhangpeng',
        'hotspot_whether_hide' => 0,
        'hotspot_connected_num' => 1,
    ],
    'system_info' => [
        'sys_bat_level' => 84,
    ],
    'network_info' => [
        'network_ip_addr' => '10.192.137.24',
    ],
];

echo "📡 MQTT主题: {$topic}\n";
echo "📊 消息类型: Vendor1协议\n\n";

// ========== 测试1: 主题识别 ==========
echo "🔍 测试1: 主题识别\n";
echo "----------------------------------------\n";

$isVendor1Topic = strpos($topic, 'tc/device/out/') === 0;
$hasVendor1Fields = isset($messageData['device_info']) || 
                   isset($messageData['hotspot_info']) ||
                   isset($messageData['system_info']);

if ($isVendor1Topic && $hasVendor1Fields) {
    echo "✅ 主题识别成功: 这是Vendor1协议的主题\n\n";
} else {
    echo "❌ 主题识别失败\n";
    exit(1);
}

// ========== 测试2: MAC地址提取 ==========
echo "🔍 测试2: MAC地址提取\n";
echo "----------------------------------------\n";

$topicParts = explode('/', $topic);
$deviceMac = $topicParts[3] ?? '';

if ($deviceMac) {
    echo "✅ MAC地址提取成功: {$deviceMac}\n\n";
} else {
    echo "❌ MAC地址提取失败\n";
    exit(1);
}

// ========== 测试3: 设备信息提取 ==========
echo "🔍 测试3: 设备信息提取\n";
echo "----------------------------------------\n";

$deviceInfo = $messageData['device_info'] ?? [];
$imei = $deviceInfo['dev_imei2'] ?? '';
$iccid = $deviceInfo['dev_iccid'] ?? '';

echo "✅ IMEI: {$imei}\n";
echo "✅ ICCID: {$iccid}\n\n";

// ========== 测试4: SIM卡信息提取 ==========
echo "🔍 测试4: SIM卡信息提取\n";
echo "----------------------------------------\n";

$simInfo = $messageData['sim_info'] ?? [];
$currentSimId = $messageData['cur_sim_id'] ?? 0;
$currentSim = $simInfo[$currentSimId] ?? [];

$signalLevel = $currentSim['sim_signal_level'] ?? 4;
$dbm = $currentSim['sim_dbm'] ?? '';

echo "✅ 当前SIM卡ID: {$currentSimId}\n";
echo "✅ 信号强度: {$signalLevel}\n";
echo "✅ dBm值: {$dbm}\n\n";

// ========== 测试5: WiFi信息提取 ==========
echo "🔍 测试5: WiFi信息提取\n";
echo "----------------------------------------\n";

$hotspotInfo = $messageData['hotspot_info'] ?? [];
$ssid = $hotspotInfo['hotspot_name'] ?? '';
$password = $hotspotInfo['hotspot_password'] ?? '';

echo "✅ WiFi SSID: {$ssid}\n";
echo "✅ WiFi密码: {$password}\n\n";

// ========== 测试6: 完整数据构建 ==========
echo "🔍 测试6: 完整数据构建\n";
echo "----------------------------------------\n";

$systemInfo = $messageData['system_info'] ?? [];
$batteryLevel = $systemInfo['sys_bat_level'] ?? 100;

$saveData = [
    'IMEI' => $imei,
    'mac' => $deviceMac,
    'iccid' => $iccid,
    'signal_strength' => $signalLevel,
    'mnc' => '移动',
    'ssid' => $ssid,
    'key' => $password,
    'main_sim' => $currentSimId,
    'user_sim' => $currentSimId,
    'band' => 0,
    'hidden_ssid' => $hotspotInfo['hotspot_whether_hide'] ?? 0,
    'conn_cnt' => $hotspotInfo['hotspot_connected_num'] ?? 0,
    'remain_pwr' => $batteryLevel,
    'dbm' => $dbm,
    'update_time' => date('Y-m-d H:i:s'),
];

echo "✅ 数据构建成功\n\n";

// ========== 显示最终结果 ==========
echo "📋 最终解析结果:\n";
echo "========================================\n";
foreach ($saveData as $key => $value) {
    echo "{$key}: {$value}\n";
}
echo "========================================\n\n";

// ========== 验证关键字段 ==========
echo "🔐 验证关键字段:\n";
echo "----------------------------------------\n";

$checks = [
    'IMEI' => ['expected' => '863874075060313', 'actual' => $saveData['IMEI']],
    'mac' => ['expected' => '00905BD7487C', 'actual' => $saveData['mac']],
    'iccid' => ['expected' => '898608551024D1344855', 'actual' => $saveData['iccid']],
    'signal_strength' => ['expected' => 5, 'actual' => $saveData['signal_strength']],
    'ssid' => ['expected' => 'zzzzzzzpppppp', 'actual' => $saveData['ssid']],
];

$allPassed = true;
foreach ($checks as $field => $check) {
    if ($check['expected'] == $check['actual']) {
        echo "✅ {$field}: {$check['actual']}\n";
    } else {
        echo "❌ {$field}: 期望 {$check['expected']}, 实际 {$check['actual']}\n";
        $allPassed = false;
    }
}

echo "\n";
if ($allPassed) {
    echo "🎉 所有验证通过！Vendor1协议报文解析成功！\n";
} else {
    echo "⚠️  部分验证失败\n";
    exit(1);
}

